package com.lzy.webapi.controller;

import com.alibaba.fastjson.JSON;
import com.lzy.framework.core.output.Result;
import com.lzy.payment.dtos.pay.AlipayPayDTO;
import com.lzy.payment.dtos.pay.CommonPayDTO;
import com.lzy.payment.dtos.pay.MobileRequestDTO;
import com.lzy.payment.dtos.pay.WeixinPayDTO;
import com.lzy.payment.pay.weixin.WxService;
import com.lzy.webapi.component.Auth;
import com.lzy.webapi.component.UserToken;
import com.lzy.webapi.facade.OrderFacade;
import com.lzy.webapi.facade.PayFacade;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName: PayController
 * @Description:该类作用域--关于第三方支付
 * @author: stone
 * @date: 2017年6月2日 下午5:22:38
 * @Copyright: 2017 www.tydic.com Inc. All rights reserved.
 */
@Controller
@RequestMapping("/api/pay")
public class PayController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    private PayFacade payFacade;

    @Autowired
    private OrderFacade orderFacade;

    /**
     * 支付宝异步请求接口：回调
     *
     * @param alipayPayDTO
     */
    @RequestMapping(value = "/alipayAsyNotify", method = RequestMethod.POST)
    @ResponseBody
    public void AlipayAsyNotify(AlipayPayDTO alipayPayDTO, HttpServletResponse httpServletResponse) {
        try {
            LOGGER.info("支付成功=====异步回调: " + JSON.toJSONString(alipayPayDTO));
            String responseContent = payFacade.alipayAsyNotify(alipayPayDTO);
            LOGGER.error("=================支付宝回调 ============================== " + JSON.toJSONString(responseContent));
            httpServletResponse.getWriter().append(responseContent);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 微信异步请求接口：回调
     *
     * @param httpServletRequest
     * @throws IOException
     */
    @RequestMapping(value = "/wxAsyNotify", method = RequestMethod.POST)
    @ResponseBody
    public void WxAsyNotify(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        try {
            JSONObject requestJson = new WxService().processNotify(httpServletRequest);
            WeixinPayDTO weixinPayDTO = (WeixinPayDTO) JSONObject.toBean(requestJson, WeixinPayDTO.class);
            LOGGER.info("====================微信支付成功异步回调 ==========================: " + JSON.toJSONString(weixinPayDTO));
            String responseContent = payFacade.alipayAsyNotify(weixinPayDTO);
            httpServletResponse.getWriter().append(responseContent);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 移动端请求后台接口 1:预付订单 2:支付状态 3:退款 4:充值  5:购买端口(成为服务商)
     *
     * @param mrDTO
     * @return
     */
    @Auth
    @RequestMapping(value = "/payRequestAll", method = RequestMethod.GET)
    @ResponseBody
    public Result payRequestAll(@RequestHeader("token") String token, MobileRequestDTO mrDTO) {
        Result result = new Result();
        try {
            UserToken ut = isLogin(token);
            mrDTO.setUserId(String.valueOf(ut.getUserId()));
            result.setResult(payFacade.payRequestAll(mrDTO));
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 公共支付接口   生成预付订单
     *
     * @param commonPayDTO
     * @return
     */
    @RequestMapping(value = "/commonPayRequest", method = RequestMethod.GET)
    @ResponseBody
    public Result commonPayRequest(@RequestHeader("token") String token, CommonPayDTO commonPayDTO) {
        Result result = new Result();
        try {
            UserToken ut = isLogin(token);
            commonPayDTO.setUserId(ut.getUserId());
            result.setResult(orderFacade.createCommonPayRequest(commonPayDTO));
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
}
